<?php
/**
 * CFK ONE Season Style Switcher for Wordpress 1.5
 * (c) by Christian F. Kaufhold, 2005 @ Mainz, Germany
 * http://cfkone.net
 *
 * PropertyReader class
 * Version: 1.0
 * 
 * Reads the xml-properties and initializes a config-hash
 */

class PropertyReader
{

	var $config = array();
	var $parser;
	var $data;

	var $attrStack = array();
	
	var $instance;

	/**
	* Constructor
	*/
	function PropertyReader($file)
	{
		$this->xmlfile = $file;
		$this->parser = xml_parser_create();
    	xml_set_object($this->parser,&$this);
       	xml_set_element_handler($this->parser, "tag_open", "tag_close");
		
		if (!($fp = fopen($this->xmlfile, "r"))) 
		{
		    die("could not open xml input-file ".$this->xmlfile);
		}

		// traversing through the xml-tree
		while ($data = fread($fp, 4096)) 
		{
		    if (!xml_parse($this->parser, $data, feof($fp))) 
		    {
		        die(sprintf("XML error: %s at line %d",
		                    xml_error_string(xml_get_error_code($this->parser)),
		                    xml_get_current_line_number($this->parser)));
		    }
		}
		xml_parser_free($this->parser);
	}
	
	/**
	* Gets an Instance of the Property-Manager for using it as a singleton
	*/
	function &getInstance($file)
	{
		static $instance;
	
		if( !isset( $instance ) )
		{
			$instance = new PropertyReader($file);
		}
		return $instance;
	}

	/**
	* handles an open-tag
	*/
	function tag_open($parser, $name, $attrs) 
	{
		array_push($this->attrStack, $attrs);
	    switch( strtoupper($name) )
	    {
	    	case "DEFAULT-STYLE":
	    		$this->config["DEFAULT-STYLE"] = $attrs["SRC"];
	    		break;
	    }
	}
	
	/**
	* handles an close-tag
	*/
	function tag_close($parser, $name) 
	{
		$config = array();
		$attrs = array_pop($this->attrStack);
	    switch( strtoupper($name) )
	    {
	   		case "SEASON":
	   			$lastconf = array_pop($this->config);
	   			$this->config["SEASONS"]{$attrs["NAME"]} = $lastconf;
	   			break;
	   		case "PERIOD":
	   			$lastconf = array_pop($this->config);
	   			$conf = array(
						"BEGIN" 		=> $attrs["BEGIN"],
						"END"			=> $attrs["END"],
						"STYLE-SHEET"	=> $lastconf
				);
				array_push($this->config, $conf);
	   			break;
	   		case "STYLE-SHEET":
	   			$conf = array(
						"SRC" => $attrs["SRC"]
	   			);
				array_push($this->config, $conf);
	   			break;
	   		case "SPECIAL-DAY":
	   			$lastconf = array_pop($this->config);
	   			$this->config["SPECIAL-DAYS"]{$attrs["NAME"]} = array(
	   				"DATE"			=> $attrs["DATE"],
					"STYLE-SHEET"	=> $lastconf
				);
	   			break;
	    }
	}
	
	/**
	* returns a node given by the xml-path or hash key
	*/
	function getNode($nodename)
	{
		$keys = preg_split("/\//", $nodename);

		if( is_array( $keys ) )
		{
			while( list( $key, $val ) = each( $keys ) )
			{
				if( isset( $this->config[$val] ) )
				{
					$result = $this->config[$val];
				}
			}
		}
		else 
		{
			$result = $this->config[$keys];
		}
		
		return $result;
	}
	
	/**
	* Returns the config-hash
	*/
	function getConfig(){
		return $this->config;
	}
}
?>
